package com.sky.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void insert(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setStatus(StatusConstant.ENABLE);
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateTime(LocalDateTime.now());
        dish.setCreateUser(BaseContext.getCurrentId());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.insert(dish);
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&& !flavors.isEmpty()){
            flavors.forEach(flavor->{
                flavor.setDishId(dishId);

            });
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public PageResult selectAll(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());

        Page<DishVO> dishPage = dishMapper.selectAll(dishPageQueryDTO);

        return new PageResult(dishPage.getTotal(),dishPage.getResult());
    }

    @Override
    @Transactional
    public void deleteByIds(List<Integer> ids) {
        //起售售中的菜品不能删除
        ids.forEach(id->{
            Dish dish = dishMapper.selectById(id);
            if(dish.getStatus()==StatusConstant.ENABLE){

                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            //被套餐关联的菜品不能删除
             List<Long> SetmealId = setmealDishMapper.selectSetmealIdByDishId(id);
            if(!SetmealId.isEmpty()){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        });
        dishMapper.deleteByIds(ids);
        dishFlavorMapper.deleteByDishIds(ids);
    }

    @Override
    public DishVO selectById(Long id) {
        DishVO dishVO = dishMapper.select(id);
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectByDishId(id);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    @Override
    public void updateAll(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dish.setUpdateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dish.setCreateTime(LocalDateTime.now());
        dish.setUpdateUser(BaseContext.getCurrentId());
        dishMapper.updateAll(dish);
        Long DishId = dish.getId();
        dishFlavorMapper.deleteByDishId(DishId);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(flavor->{
            flavor.setDishId(DishId);
        });
        if(flavors!=null&& !flavors.isEmpty()){
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public void updateStatusById(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.updateAll(dish);
    }

    @Override
    public List<Dish> selectByCategoryId(Long categoryId) {
        List<Dish> dishList = dishMapper.selectByCategoryId(categoryId);
        return dishList;
    }
}
